package com.xywy.bloodandroid.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.TextView;

import com.xywy.bloodandroid.R;

/**
 * 我 - 个人资料 - 个人头像裁剪
 */
public class ClipIconPopupWindow {

	private TextView btnPhoto, btnAlbum, btnCancel;
	private View mMenuView;
	private int width, height;
	private Dialog dialog;

	public ClipIconPopupWindow(Activity context, OnClickListener itemsOnClick) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		width = wm.getDefaultDisplay().getWidth() / 2;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		dialog = new Dialog(context, R.style.Translucent_NoTitle);
		mMenuView = inflater.inflate(R.layout.me_infor_clip_icon, null);
		btnPhoto = (TextView) mMenuView
				.findViewById(R.id.btn_me_infor_clip_photo);
		btnAlbum = (TextView) mMenuView
				.findViewById(R.id.btn_me_infor_clip_album);
		btnCancel = (TextView) mMenuView
				.findViewById(R.id.btn_me_infor_clip_cancel);
		// 取消按钮
		btnCancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// 销毁弹出框
				dialog.dismiss();
			}
		});
		// 设置按钮监听
		btnPhoto.setOnClickListener(itemsOnClick);
		btnAlbum.setOnClickListener(itemsOnClick);
		android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
				dip2px(context, 200), dip2px(context, 150));
		// 设置SelectPicPopupWindow的View
		dialog.setContentView(mMenuView, params);
		// 设置SelectPicPopupWindow弹出窗体的宽
		// dialog.setWidth(dip2px(context, 200));
		// 设置SelectPicPopupWindow弹出窗体的高
		// dialog.setHeight(dip2px(context, 150));
		// 设置SelectPicPopupWindow弹出窗体可点击
		// dialog.setFocusable(true);

		// 设置SelectPicPopupWindow弹出窗体动画效果
		// this.setAnimationStyle(R.style.AnimBottom);
		// 实例化一个ColorDrawable颜色为半透明
		// ColorDrawable dw = new ColorDrawable(0xb0000000);
		// 设置SelectPicPopupWindow弹出窗体的背景
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.pop_layout).getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dialog.dismiss();
					}
				}
				return true;
			}
		});

	}

	public void show() {
		if (dialog != null)
			dialog.show();
	}

	public void dismiss() {
		if (dialog != null && dialog.isShowing())
			dialog.dismiss();
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
}
